import paho.mqtt.client as mqtt
import time,sys

sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
# MQTT 服务器地址和端口
broker_address = "127.0.0.1"
broker_port = 1883

# 回调函数：当客户端连接到 MQTT 服务器时触发
def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to MQTT broker")
    else:
        print("Failed to connect, return code %d\n", rc)

# 创建 MQTT 客户端实例
client = mqtt.Client()

# 设置回调函数
client.on_connect = on_connect

# 连接到 MQTT 服务器
client.connect(broker_address, broker_port)

client.publish("test/333", "dddd")
# 发送 100 万条消息
# a=0
# while 1:
#     a+=1
#     message = f"Message {a}"
#     client.publish("/test", message)
#     time.sleep(1000)
    # 控制发送速度，避免过快导致服务器压力过大

# 等待所有消息发送完成
client.loop_start()
client.loop_stop()
while 1:pass